from django.conf import settings
from django.core.validators import FileExtensionValidator
from django.db import models

from .validators import validate_file_size


class Category(models.Model):
    name = models.CharField(max_length=255)
    category_type = models.IntegerField()
    # creator = models.BigIntegerField(null=True)
    creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True)

    remark = models.CharField(max_length=255, null=True, blank=True)
    """备注"""

    valid = models.BooleanField(default=True)
    sort = models.IntegerField(null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    parent_id = models.BigIntegerField(null=True)
    level = models.IntegerField(null=True)


class CategoryImage(models.Model):
    category = models.ForeignKey(
        Category, on_delete=models.CASCADE, related_name="images"
    )
    image = models.ImageField(upload_to="demo/images", validators=[validate_file_size])


class CategoryJson(models.Model):
    category = models.ForeignKey(
        Category, on_delete=models.CASCADE, related_name="json_files"
    )
    json = models.FileField(
        upload_to="demo/json",
        validators=[FileExtensionValidator(allowed_extensions=["json"])],
    )
